<?php
require_once 'abstractModel.php';

class AlbumModel extends AbstractModel{
	//TABLE NAMES
	const TABLE_NAME_ALBUM = 'tAlbum';
	const TABLE_NAME_PICTURE = 'tPicture';

	//ALBUM
	const ALBUM_INDEX = 'AIndex';
	const ALBUM_USER_ID = 'userId';
	const ALBUM_TITLE = 'ATitle';
	const ALBUM_CREATED_DATE = 'ACreatedDate';
	const ALBUM_REFRESHED_DATE = 'ARefreshedDate';
	const ALBUM_CONTENT = 'AContent';

	//PICTURE
	const PICTURE_INDEX = 'PIndex';
	const PICTURE_USER_ID = 'userId';
	const PICTURE_ALBUM_INDEX = 'AIndex';
	const PICTURE_PATH = 'PPath';
	const PICTURE_CREATED_DATE = 'PCreatedDate';
	const PICTURE_CONTENT = 'PContent';

	public function __construct(){
		parent::__construct();
		$this->targetTable = self::TABLE_NAME_ALBUM;
	}

	/**
	 * @name setTargetTable
	 * @param $tableName
	 * @param AlbumModel::TABLE_NAME_ALBUM or AlbumModel::TABLE_NAME_PICTURE
	 */

	public function setTargetTable($tableName){
		$this->targetTable=$tableName;
	}
	/**
	 * (non-PHPdoc)
	 * @see AbstractModel::insert()
	 */

	public function insert(){
		$userId = func_get_arg(0);
		$aTitle = func_get_arg(1);
		if ($this->targetTable==self::TABLE_NAME_ALBUM){
			$sth = $this->db->prepare("Insert into {$this->targetTable}(userId, ATitle, AContent, ARefreshedDate)
			values(?, ?, ?, NOW())");
			$aContent = func_get_arg(2);
			$sth->bindParam(1, $userId);
			$sth->bindParam(2, $aTitle);
			$sth->bindParam(3, $aContent);
		} else {
			$sth = $this->db->prepare("Insert into {$this->targetTable}(userId, AIndex, PPath, PContent)
				values(?, ?, ?, ?)");
			$pPath = func_get_arg(2);
			$pContent = func_get_arg(3);
			$sth->bindParam(1, $userId);
			$sth->bindParam(2, $aTitle);
			$sth->bindParam(3, $pPath);
			$sth->bindParam(4, $pContent);
		}
		return $sth->execute();
	}

	public function select($sth){
		return parent::select($sth);
	}

	/**
	 * (non-PHPdoc)
	 * @see AbstractModel::update()
	 */

	public function update(){
		if ($this->targetTable==self::TABLE_NAME_ALBUM){
			$sth = $this->db->prepare("Update {$this->targetTable}
				SET ATitle=?, ARefreshedDate=now(), AContent=? WHERE AIndex=?");
			$sth->bindParam(1, func_get_arg(0));
			$sth->bindParam(2, func_get_arg(1));
			$sth->bindParam(3, func_get_arg(2));
		} else {
			$sth = $this->db->prepare("Update {$this->targetTable}
			SET PPath=?, PContent=? WHERE PIndex=?");
			$sth->bindParam(1, func_get_arg(0));
			$sth->bindParam(2, func_get_arg(1));
			$sth->bindParam(3, func_get_arg(2));
		}
		return $sth->execute();
	}

	/**
	 * (non-PHPdoc)
	 * @see AbstractModel::delete()
	 * @todo  ALBUM情報をDB,TABLE(tAlbum) or PICTURE情報をDB TABLE(tPicture) deleteするメッソド
	 * @param 下の1つのparameterが必要
	 * @param 1.(String) AIndex or PIndex
	 * @param
	 * @tutorial
	 * //ALBUM
	 * $a = new AlbumModel();
	 * $a->setTargetTable(AlbumModel::TABLE_NAME_ALBUM);
	 * print $a->delete("1");
	 *
	 * //PICTURE
	 * $a = new AlbumModel();
	 * $a->setTargetTable(AlbumModel::TABLE_NAME_PICTURE);
	 * print $a->delete("1");
	 * @return boolean
	 */

	public function delete($where){
		$tableIndex = $this->targetTable==self::TABLE_NAME_ALBUM ? self::ALBUM_INDEX : self::PICTURE_INDEX;
		$sth = $this->db->prepare("Delete from {$this->targetTable} where {$tableIndex} = ?");
		$sth->bindParam(1, $where);
		return $sth->execute();
	}
}
